package com.monster.dict.web;

import com.monster.dict.domain.common.DictItemValidGroup;
import com.monster.dict.domain.dto.DictItemInsertDto;
import com.monster.dict.domain.dto.DictItemUpdateDto;
import com.monster.dict.domain.service.SysDictItemService;
import com.monster.dict.domain.vo.DictItemListVo;
import com.monster.valid.annotation.ValidMonster;
import com.monster.valid.regex.RegexEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * pc 字典项 视图层
 *
 * @author kuang
 */
@Validated
@RestController
@RequestMapping("dictItem")
public class SysDictItemController {
    /**
     * 字典项的service层
     */
    @Autowired
    private SysDictItemService dictItemService;

    /**
     * 通过字典编码查询字典项
     *
     * @param dictCode 字典编码
     * @return List<DictItemListVo>
     */
    @GetMapping("list/{dictCode}")
    public List<DictItemListVo> list(@PathVariable String dictCode) {
        return dictItemService.list(dictCode);
    }

    /**
     * 新增字典项数据
     *
     * @param insertDto 新增的数据
     * @return String
     */
    @PostMapping("insert")
    public String insert(@RequestBody @Validated(DictItemValidGroup.Insert.class) DictItemInsertDto insertDto) {
        return dictItemService.insert(insertDto);
    }

    /**
     * 修改数据
     *
     * @param updateDto 需要修改的数据
     * @return boolean
     */
    @PostMapping("update")
    public boolean update(@RequestBody @Validated(DictItemValidGroup.Update.class) DictItemUpdateDto updateDto) {
        return dictItemService.update(updateDto);
    }

    /**
     * 通过id删除数据
     *
     * @param id 数据id
     * @return boolean
     */
    @GetMapping("del/{id}")
    public boolean delete(@PathVariable @ValidMonster(name = "数据id", isNull = false, max = 25, min = 15) String id) {
        return dictItemService.delete(id);
    }

    /**
     * 通过字典编码删除批量
     *
     * @param dictCode 字典编码
     * @return boolean
     */
    @GetMapping("delByDictCode/{dictCode}")
    public boolean deleteByDictCode(@PathVariable @ValidMonster(name = "字典编码", value = RegexEnum.ENGLISH_SMALL, specials = "_", isNull = false, max = 32, groups = DictItemValidGroup.Group.Code.class) String dictCode) {
        return dictItemService.deleteByDictCode(dictCode);
    }


}
